package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class HexunNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		// http://news.hexun.com/2012-11-22/148219409.html
		if (url.startsWith("http://tv.")) {
			return false;
		}
		boolean bRet=false;

		String[] regex={ 
				"http://.*.hexun.com.tw/[0-9]*-[0-9]*-[0-9]*/[0-9]*.html",
				"http://.*[.]hexun[.]com/[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9]+[_0-9]*[.]html" ,
				"http://.*[.]hexun[.]com/[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9]+[_0-9]*[.]html\\?from.*=.*" ,
		};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";
		String url = urlMeta.getUrl();
		Long date=null;
		String source="和讯网";
		int commentNum=0;
		int clickNum=0;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(urlMeta.getUrl());
		meta.setType(0);
		Document doc=Jsoup.parse(html);
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		title=doc.select("div#artibodyTitle>h1").text();
		if (StringUtils.isBlank(title)) {
			title=doc.select("div.slide_title>h1").text();
		}

		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate(doc.select("span#artibodyDesc").text());
		if (date == null) {
			date=DateUtils.matchDate(doc.select("div#artInfo")
					.text());
			if (date == null) {
				date=DateUtils.matchDate(doc.select("div#artibodyTitle")
						.text());
				if (date == null) {
					date=DateUtils.matchDate(doc.select("div#photoTime")
							.text());
				}
			}

		}

		/*
		 * 解析内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div#artibody"));
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div#slide_subtitle2"));
		}

		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);

		String author=doc.select("div.title_desc span#artibodyDesc a").text();
		meta.setAuthor(author);
		return meta;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	private final static Pattern COMMENTSCRIPT=Pattern
			.compile("showComment_[0-9]*\\([0-9]*,[0-9]*\\)");


	public static void main(String[] args) {

		String url="http://stock.hexun.com/2014-05-14/164765956.html?from=rss";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		HexunNewsAnalyse aa=new HexunNewsAnalyse();
		boolean detailPage=aa.isDetailPage(url);
		if (detailPage) {
			NewsMeta parserHtml=aa.parserHtml(urlMeta);
			System.out.println(parserHtml);
//			System.out.println(aa.Update(parserHtml));
		} else {
			System.out.println("不符合正则");
		}
	}

	
	public boolean isNeedUpdate(){
		return true;
	}
}
